import express from 'express'
// const cors = require('cors')
import loginRouter from './src/controller/login'
import userRouter from './src/controller/user'
import uploadRouter from "./src/controller/upload";
import fileRouter from './src/controller/file'
import calendarRouter from './src/controller/calendar'
import recentlyRouter from "./src/controller/recently";
import expertRouter from './src/controller/expert'
import './src/utils/socket'
import verify from './src/utils/verify'

const app = express()
// app.use(cors)
// 设置跨域访问
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*'); // 允许任何源
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); // 允许的HTTP方法
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); // 允许的HTTP请求头

    if (req.method === 'OPTIONS') {
        res.end();
    } else {
        next();
    }
});
app.use(express.static('./src/public'))
// 解析post请求参数
app.use(express.json())
app.use(express.urlencoded({ extended: true }));

app.use(verify)
// 路由模块开发
app.use(loginRouter)
app.use('/user', userRouter)
app.use('/upload', uploadRouter)
app.use('/file', fileRouter)
app.use('/calendar', calendarRouter)
app.use('/recently', recentlyRouter)
app.use('/expert', expertRouter)

app.listen(process.env.NODE_SERVER_PORT, () => {
    console.log(`http://localhost:${process.env.NODE_SERVER_PORT}`)
})